<template>
  <div id="app">
    <a-form :model="searchParams">
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="value1" label="标题" label-col-flex="100px">
            <a-input
              v-model="searchParams.title"
              placeholder="please enter..."
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="value2" label="描述" label-col-flex="80px">
            <a-input
              v-model="searchParams.content"
              placeholder="please enter..."
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="value3" label="标签" label-col-flex="80px">
            <a-input-tag
              v-model="searchParams.tags"
              :style="{ width: '320px' }"
              placeholder="按Enter键入题目标签"
              allow-clear
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <a-table
      :columns="columns"
      :data="data"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <!--      <template #judgeConfig="{ record }">-->
      <!--        <a-space>-->
      <!--          {{ JSON.stringify(record.judgeConfig) }}-->
      <!--        </a-space>-->
      <!--</template>-->
      <template #tags="{ record }">
        <a-tag v-for="item of record.tags" :key="item">{{ item }}</a-tag>
      </template>
      <template #judgeCases="{ record }">
        <a-space>
          <a-upload
            :auto-upload="false"
            :limit="1"
            ref="uploadRef"
            :custom-request="customRequest"
          >
            <template #upload-button>
              <a-space>
                <a-button> select file</a-button>
              </a-space>
            </template>
          </a-upload>
          <a-button type="primary" @click="uploadCases(record)">
            上传
          </a-button>
        </a-space>
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="handleUpdate(record)">更新</a-button>
          <a-button type="primary" status="danger" @click="handleDelete(record)"
            >删除
          </a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import { useRouter } from "vue-router";
import {
  QuestionControllerService,
  QuestionQueryRequest,
  QuestionVo,
} from "@/service";
import { Message, RequestOption } from "@arco-design/web-vue";
import useState from "@arco-design/web-vue/es/_hooks/use-state";

const [uploadId, setUploadId] = useState<number>(0);

const customRequest = (option: RequestOption) => {
  const { onProgress, onError, onSuccess, fileItem, name } = option;
  QuestionControllerService.uploadJudgeCases(
    uploadId.value,
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    //@ts-ignore
    { file: fileItem?.file }
  );
};
const data = ref<QuestionVo[]>([]);
const router = useRouter();
const uploadRef = ref();
const uploadCases = (record: QuestionVo) => {
  console.log(record);
  setUploadId(record.id ?? 0);
  uploadRef.value.submit();
};
const columns = [
  {
    title: "题目编号",
    dataIndex: "id",
    width: 140,
  },
  {
    title: "题目",
    dataIndex: "title",
    ellipsis: true,
  },
  {
    title: "内容",
    dataIndex: "content",
    ellipsis: true,
  },
  // {
  //   title: "判题设置",
  //   slotName: "judgeConfig",
  //
  //   // ellipsis: true,
  // },
  {
    title: "题目标签",
    slotName: "tags",
    ellipsis: true,
  },
  {
    title: "提交数",
    dataIndex: "submitNum",
    width: 120,
    minWidth: 80,
  },
  {
    title: "通过数",
    dataIndex: "acceptedNum",
    width: 120,
    minWidth: 80,
  },
  {
    title: "测试用例文件",
    slotName: "judgeCases",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];
const searchParams = ref<QuestionQueryRequest>({
  pageSize: 4,
  page: 1,
  title: undefined,
  content: undefined,
  tags: [],
});
const total = ref(0);
const handleUpdate = (question: QuestionVo) => {
  router.push({
    path: "/question/update",
    query: {
      id: question.id,
    },
  });
};
const handleDelete = async (question: QuestionVo) => {
  const res = await QuestionControllerService.deleteQuestion({
    id: question.id,
  });
  if (res.code != 0) {
    Message.error("删除失败：" + res.message);
    return;
  }
  Message.success("删除成功");
  await loadData();
};
const loadData = async () => {
  const res = await QuestionControllerService.listQuestionVoByPage(
    searchParams.value
  );
  if (res.code !== 0) {
    Message.error("数据加载失败：" + res.message);
    return;
  }
  data.value = res?.data?.records ?? [];
  total.value = res.data?.total ?? 0;
};
const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    page: page,
  };
};

onMounted(() => {
  loadData();
});
watchEffect(() => {
  loadData();
});
</script>

<style scoped>
#app {
  margin: 20px 10px 0 10px;
}
</style>
